﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04_IOC
{

    internal interface IMessageProvider
    {
        void Send(string message);
    }

    internal class EmailMessageProvider : IMessageProvider
    {
        public void Send(string message)
        {
            Console.WriteLine("邮箱发送：" + message);
        }
    }
    internal class PhoneMessageProvider : IMessageProvider
    {
        public void Send(string message)
        {
            Console.WriteLine("手机发送：" + message);
        }
    }
    internal class MessageManager
    {
        readonly IEnumerable<IMessageProvider> _providers;
        public MessageManager(IEnumerable<IMessageProvider> providers)
        {
            _providers = providers;
        }
        public void SendAll(string message)
        {
            foreach (var provider in _providers)
            {
                provider.Send(message);
            }
        }
        public void SendPhone(string message)
        {
            foreach (var provider in _providers)
            {
                if (provider is PhoneMessageProvider)
                {
                    provider.Send(message);
                }
            }
        }
        public void SendEmail(string message)
        {
            foreach (var provider in _providers)
            {
                if (provider is EmailMessageProvider)
                {
                    provider.Send(message);
                }
            }
        }
    }
}
